package com.google.code.sysarch.common.util;

/*
 * ============================================================================
 * file:	$RCSfile: GraphicBufferThreadA.java,v $
 * created by:Frank Drewek
 *
 * last modification at $Date: 1997/11/06 18:00:59 $
 *		   by $Author: drewek $
 *
 * (c)	Universitaet Karlsruhe
 *	Institut fuer Betriebs- und Dialogsysteme, Lehrstuhl Betriebssysteme
 *	Am Fasanengarten 5, 76128 Karlsruhe
 *
 * Permission to use this software for non-profit educational purposes is
 * hereby granted. All other rights are reserved. This software is provided
 * "as is", without any express or implied warranty.
 * ============================================================================
 */

// package threads;

import java.awt.*;

import com.google.code.sysarch.v0.e37.ThreadBuffer;


/**
 * GraphicBufferThreadA is a thread-class that manages the graphical output
 * of the buffer from class ThreadBuffer. It interprets the values
 * of the buffer as the length of a filled rectangle and prints the
 * rectangles on the screen.
 * It refreshes the screen isochronously.
 *
 * @author    Frank Drewek
 * @author    Juergen Schneider
 * @see       ThreadTA5A
 * @see       ThreadTA5B
 * @see       Buffer
 * @see       ThreadBuffer 
 * @see       ProductThread
 * @see       GraphicBufferThreadB
 */
public class GraphicBufferThreadA2 extends Canvas
    implements Runnable
 {
   ////////////////////////////////////////////////////////////////////////////
   // attributes
   ////////////////////////////////////////////////////////////////////////////
   
   /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

/** Time (millisec) the threads sleep between two outputs.*/
   private long time;
   
   /** central buffer-object */
   public static ThreadBuffer threadBuffer;
   
   /** object for double buffering */
   private Image offscreenImage;
   
   /** Object that contains the graphics. It is used for double bufferung. */
   private Graphics offscreenGraph;
   
   /** Object that contains the panel. */
   private Frame frame;
   
   /** Object that contains the graphics. */
   private Panel panel;
   
   /** width of the canvas */
   private int width;
   
   /** height of the canvas */
   private int height;
   
   /** white color */
   static final Color WHITE = Color.white;
   
   /** red color */
   static final Color RED = Color.red;

   static final Color BLUE = Color.blue;
   static final Color GRAY = Color.gray;
   
   /** state is true if the object for double buffering is initialized. */
   private boolean state;
   
   ////////////////////////////////////////////////////////////////////////////
   // constructors
   ////////////////////////////////////////////////////////////////////////////
   
   /**
    * The constructor GraphicBufferThreadA initializes the width and the height
    * of the frame that will be printed on the screen.
    * It also initializes the time that the thread sleeps between two outputs.
    * It gets the buffer from which it takes the length for the rectangles.
    * It creates a frame that shows the graphics on the screen.
    * @param b   central object from class ThreadBuffer
    * @param wi  width of the canvas
    * @param hi  height of the canvas
    * @param t   time the thread sleeps between outputs
 * @return 
    */
   public void GraphicBufferThreadA(long t, ThreadBuffer b, int wi, int hi)
     {
       time = t;
       threadBuffer = b;
       width = wi;
       height = hi;
       setSize(width, height);
       setBackground(WHITE);
	   
       state = true;
       frame = new Frame("View");
       frame.setSize(width+20, height+20);
	   
       panel = new Panel();
       panel.setLayout(new GridLayout(1, 1));
       panel.add(this);
	   
       frame.add(panel);
       frame.setVisible(true); 
     }
 

   ////////////////////////////////////////////////////////////////////////////
   // methods
   ////////////////////////////////////////////////////////////////////////////

   // access //////////////////////////////////////////////////////////////////


   // service /////////////////////////////////////////////////////////////////

   /**
    * run refreshes the screen isochronously until the central buffer
    * is empty.
    */
	public void run()
	{
		do {
			try {
				Thread.sleep(time);
			} catch (Exception e) {
				System.out.println("Pech gehabt.");
				System.exit(0);
			}
			// System.out.println("Repaint.");
			if (frame.isShowing()) {
				repaint();
			}
		} while (!threadBuffer.isEmpty());
		frame.dispose();
	}


   /** paint shows the graphics on the screen. */
   public void paint(Graphics g)
     {
       if (state == true)
	 {
	   offscreenImage = createImage(width, height);
	   offscreenGraph = offscreenImage.getGraphics();
           state = false;
         }
       
       for (int i=0; i < threadBuffer.getMax(); i++)
         {
           offscreenGraph.setColor(GRAY);
           offscreenGraph.fillRect((13 + 30*i), 13, 20, threadBuffer.getBuffer(i));
           if (threadBuffer.getIOBuffer(i)) {
			offscreenGraph.setColor(BLUE);
		} else {
			offscreenGraph.setColor(RED);
		}
           offscreenGraph.fill3DRect((10 + 30*i), 10, 20, threadBuffer.getBuffer(i), true);
         }   

       g.drawImage(offscreenImage, 0, 0, this);
     }
   
   /**
    * update invokes the paint-method 
    * @param g    the graphics that will be printed
    */
   public void update(Graphics g)
     {
       offscreenGraph.setColor(WHITE);
       offscreenGraph.fillRect(0, 0, width, height);
       paint(g);
     }

   // debug ///////////////////////////////////////////////////////////////////


   /** @deprecated	for debugging purposes */
   public final static String revision_GraphicBufferThread =
     "$Id: GraphicBufferThreadA.java,v 1.2 1997/11/06 18:00:59 drewek Exp drewek $";

   /** @deprecated	for debugging purposes */
   public String getRevision()
     { return revision_GraphicBufferThread; }
 }



 
